﻿Type.registerNamespace('MyCompany');
MyCompany.customObject = function(ns, methodId)
{
    MyCompany.customObject.initializeBase(this);

    this.ns = ns;   //store namespace for controls within this webcontrol
    this.methodId = methodId; //store methodid registered in code-behind
    this._successDelegate = null; //reference to delegate used to handle successful callback
    this._failDelegate = null; //reference to delegate used to handle failed callback

    this.div = $get(this.ns + 'divTime'); //obtain div tag to report results on
    this.btn = $get(this.ns + 'btnGetTime');    //obtain button used to invoke callbacks
    $addHandlers(this.btn, {'click': this.getTime}, this); //attach event handler to button's click event
}

MyCompany.customObject.prototype = 
{
    getTime: function(evt)
    {
        this._setupDelegates();
        dnn.xmlhttp.callControlMethod('MyCompany.MyClass3.' + this.methodId, 'GetDateTime', null, this._successDelegate, this._failDelegate, null);   
        evt.stopPropagation();
        return false;   //cancel button postback
    },

    success: function(payload, ctx, req)
    {
        this.reportTime(payload);
    },
    
    fail: function(payload, ctx, req)
    {
        alert('error: ' + payload);    
    },
    
    reportTime: function(time)
    {
        this.div.innerHTML = time;
    },
    
    dispose: function()
    {
        this._successDelegate = null;
        this._failDelegate = null;
        this.div = null;
        $clearHandlers(this.btn);
        this.btn = null;
        MyCompany.customObject.callBaseMethod(this, 'dispose');
    },
    
    _setupDelegates: function()
    {
        if (this._successDelegate == null)
        {
            this._successDelegate = Function.createDelegate(this, this.success);
            this._failDelegate = Function.createDelegate(this, this.fail);
        }
    }
}
MyCompany.customObject.registerClass('MyCompany.customObject', Sys.Component);

